<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/13
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Dynno extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                's_admin' => 3
            ];
            if(isset($user_name) && $user_name != ''){
                $where['s_u_name|s_u_phone'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($user_name1) && $user_name1 != ''){
                $where['s_com_name|s_name|s_phone|s_phone1|s_phone2'] = ['like', '%' . $user_name1 . '%'];
            }
            if(isset($user_name2) && $user_name2 != ''){
                $where['s_inside_name|s_inside_phone'] = ['like', '%' . $user_name2 . '%'];
            }
            if(isset($s_verify) && $s_verify != ''){
                $where['s_verify'] = $s_verify;
            }
            if(isset($s_status) && $s_status != ''){
                $where['s_status'] = $s_status;
                if($s_status == 3){
                    $where['s_status'] = ['>=',3];
                }
            }
            if(isset($province) && $province != ''){
                $where['s_sheng'] = $province;
            }
            if(isset($city) && $city != ''){
                $where['s_shi'] = $city;
            }
            if(isset($district) && $district != ''){
                $where['s_qu'] = $district;
            }
            if(isset($address) && $address != ''){
                $where['s_address|s_area_str'] = ['like', '%' . $address . '%'];
            }
            if(isset($s_admin) && $s_admin != ''){
                $where['s_admin'] = $s_admin;
            }
            if(isset($s_protect) && $s_protect != ''){
                $where['s_protect'] = $s_protect;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_shop')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_shop')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('s_status asc,s_verify asc,s_update_time desc')
                ->select();
            if($lists){
                foreach ($lists as $k => $v){
                    $lists[$k]['red_son'] = 0;
                    $have = Db::table('up_shop_image')
                        ->where('si_s_id',$v['s_id'])
                        ->where('si_status',1)
                        ->value('si_id');
                    if($have){
                        $lists[$k]['red_son'] = 1;
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $province = Db::table('up_area')->where('level', 1)->select();
        $this->assign([
            'province' => $province,
        ]);
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed
     */
    public function add(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $lalo = explode(',',$lonlat);
            $a1 = Db::table('up_area')->where('district_id', $s_sheng)->value('district');
            $a2 = Db::table('up_area')->where('district_id', $s_shi)->value('district');
            $a3 = Db::table('up_area')->where('district_id', $s_qu)->value('district');
            $update = [
                's_u_name' => $s_u_name,
                's_u_phone' => $s_u_phone,
                's_u_userid' => 0,
                's_com_name' => $s_com_name,
                's_name' => $s_name,
                's_phone' => $s_phone,
                's_phone1' => $s_phone1,
                's_phone2' => $s_phone2,
                's_inside_name' => $s_inside_name,
                's_inside_phone' => $s_inside_phone,
                's_verify' => 2,
                's_status' => 2,
                's_time' => $today,
                's_endtime' => $today,
                's_update_time' => $today,
                's_address' => $s_address,
                's_sheng' => $s_sheng,
                's_shi' => $s_shi,
                's_qu' => $s_qu,
                's_area_str' => $a1 . ' ' . $a2 . ' ' . $a3,
                's_lon' => $lalo[1],
                's_lat' => $lalo[0],
                's_foradmin_time' => $today,
                's_admin' => 3,
                's_year_other' => $s_year_other,
                's_time_other' => $s_time_other,
                's_done_other' => $s_done_other,
            ];
            $res = Db::table('up_shop')->insert($update);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $province = Db::table('up_area')->where('level', 1)->select();
        $this->assign([
            'province' => $province,
        ]);
        return $this->fetch();
    }

    /**
     * 编辑
     * @return mixed
     */
    public function update(){
        if(Request::instance()->isPost()){
            $s_id = input('s_id');
            $s_com_name = input('s_com_name');
            $s_name = input('s_name');
            $s_inside_name = trim(input('s_inside_name'), ' ');
            $s_inside_phone = input('s_inside_phone');
            $s_year_other = input('s_year_other');
            $s_time_other = input('s_time_other');
            $s_done_other = input('s_done_other');
            $update = [
                's_com_name' => $s_com_name,
                's_name' => $s_name,
                's_inside_name' => $s_inside_name,
                's_inside_phone' => $s_inside_phone,
                's_year_other' => $s_year_other,
                's_time_other' => $s_time_other,
                's_done_other' => $s_done_other,
                's_foradmin_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_shop')->where('s_id', $s_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $s_id = input('s_id');
        $data = Db::table('up_shop')
            ->where('s_id', $s_id)
            ->find();
        $this->assign('data', $data);
        return $this->fetch();
    }
    /**
     * 删除帖子
     * @return \think\response\Json
     */
    public function dynDel(){
        $id = input('id');
        Db::startTrans();
        try{
            Db::table('up_shop')->where('s_id', $id)->delete();
            Db::table('up_shop_image')->where('si_s_id', $id)->delete();
            Db::commit();
            return json(['code' => 200, 'msg' => '成功']);
        }catch (\Exception $e) {
            Db::rollback();
            return json(['code' => 100, 'msg' => '失败']);
        }
    }

    /**
     * 导出Excel
     * @return \think\response\Json
     */
    public function outExcel(){
        extract(input());
        set_time_limit(0);//防止超时
        //ini_set("memory_limit", "512M");//防止内存溢出
        $where = [
            's_admin' => 3
        ];
        if(isset($user_name) && $user_name != ''){
            $where['s_u_name|s_u_phone'] = ['like', '%' . $user_name . '%'];
        }
        if(isset($user_name1) && $user_name1 != ''){
            $where['s_com_name|s_name|s_phone|s_phone1|s_phone2'] = ['like', '%' . $user_name1 . '%'];
        }
        if(isset($user_name2) && $user_name2 != ''){
            $where['s_inside_name|s_inside_phone'] = ['like', '%' . $user_name2 . '%'];
        }
        if(isset($s_verify) && $s_verify != ''){
            $where['s_verify'] = $s_verify;
        }
        if(isset($s_status) && $s_status != ''){
            $where['s_status'] = $s_status;
            if($s_status == 3){
                $where['s_status'] = ['>=',3];
            }
        }
        if(isset($province) && $province != ''){
            $where['s_sheng'] = $province;
        }
        if(isset($city) && $city != ''){
            $where['s_shi'] = $city;
        }
        if(isset($district) && $district != ''){
            $where['s_qu'] = $district;
        }
        if(isset($address) && $address != ''){
            $where['s_address|s_area_str'] = ['like', '%' . $address . '%'];
        }
        if(isset($s_admin) && $s_admin != ''){
            $where['s_admin'] = $s_admin;
        }
        if(isset($s_protect) && $s_protect != ''){
            $where['s_protect'] = $s_protect;
        }
        if($ids){
            if($ids != 'all'){
                $where['s_id'] = ['in',$ids];
            }
        }else{
            return json(['code'=>0,'msg'=>'请选择数据！']);
        }
        $field = ['s_u_name','s_u_phone','s_com_name','s_name', 's_phone',
            's_phone1','s_phone2','s_verify','s_reason','s_status','s_time','s_endtime',
            's_update_time','s_inside_name','s_inside_phone','s_area_str','s_address',
            's_lon','s_lat','s_days','s_admin','s_year_other','s_time_other','s_done_other'
        ];
        $data = Db::table('up_shop')
            ->where($where)
            ->order('s_status asc,s_verify asc,s_update_time desc')
            ->field($field)
            ->select();
        if($data){
            $cellname = [
                '报备人', '报备人电话', '店名', '联系人', '联系电话1',
                '联系电话2', '联系电话3', '审核状态', '拒绝原因(如拒绝)', '加盟状态', '提交时间', '有效截止时间',
                '更新时间', '老师名字', '老师电话', '地址', '地址详情',
                '经度', '纬度', '保护天数', '数据类型', '年任务量','保护时间','是否完成当年任务',
            ];
            $status = ['s_verify','s_status','s_admin'];
            $values = [
                's_verify' => [1 => '待审核',2 => '审核通过',3 => '审核拒绝'],
                's_status' => [1 => '待加盟',2 => '已加盟',3 => '已过更新有效期',4 => '已到期'],
                's_admin' => [1 => '前台报备数据',2 => '后台添加真实数据',3 => '后台添加假数据']
            ];
            $res = exportExcel('shop',time(),$cellname,$data,$status,$values);
            return json($res);
        }else{
            return json(['code'=>0,'msg'=>'此条件下无数据']);
        }
    }
}